﻿using DAL.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Models;
using Microsoft.EntityFrameworkCore;

namespace DAL.Dao
{
    public class MessageDaoimpl: BaseDao, IMessageDao
    {

        public MessageDaoimpl(bool use_clock = true) : base(use_clock)
        {

        }
        public int ExistNum { get { using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions)) return context.Message.Count(); } private set { } }


        public Message? GetMessageInfo(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return  (from i in context.Message where i.Id==id select i).ToList().FirstOrDefault();
        }
        public IList<Message> GetMessageInfoList(int userID)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Message where i.UserId == userID select i ).ToList();
        }
        public (int,IList<Message>) GetList(int rows,int page)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                var rs = (from i in context.Message select i).ToList();
                return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
            }
              
        }
        public IList<Message> GetList()
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Message select i).ToList();
        }

        public (int, IList<Message>) GetMessageInfoList(int userID, int rows, int page)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                var rs = (from i in context.Message where i.UserId == userID select i).ToList();
                return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
            }
                
        }



        public int Add(Message message)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_add)
                {

                    //var seq = GetList();
                    //if (seq.Count == 0) message.Id = 0;
                    //else
                    //{
                    //    message.Id = seq.Last().Id + 1;
                    //}

                 
                        context.Message.Add(message);
                        context.SaveChanges();
                    
                    return message.Id;
                }
            }
              

        }

        public bool Update()
        {
            lock (clock_obj_update)
            {
                return true;
            }
            
        }
        public bool Delete(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_delete)
                {
                    Message? message = GetMessageInfo(id);
                    if (message == null) return false;
                    context.Message.Remove(message);
                    context.SaveChanges();
                    return true;
                }
            }
               

        }
      
    }
}
